<?php

include_once(APP_URL.'protected/lib/private/Check.php');
include_once(APP_URL.'protected/lib/private/Mailer.php');
include_once(APP_URL.'protected/lib/private/Regular.php');

class UserController extends BaseController {

    public function init() {

        $this->layout = '//layouts/layout_developer';
    }

    public function actionLogin() {
        // http://192.168.3.69/user/login

        if(!$this->session()) {
            $this->render('login');
            exit;
        }
        $this->redirect(URL_PROJECT_INDEX);
    }

    public function actionLoginPost() {

        $email = Check::checkPostParam(-1, 'email', false);
        $password = Check::checkPostParam(-1, 'password', false);
        if(!Regular::regEmail($email)) {
            echo Resp::retFail(-1, 'invalid email not right email format');
            exit;
        }

        $condArr = array(
            'select' => array('id', 'email'),
            'condition' => 'email=:email AND password=:password AND verified=:verified',
            'params' => array('email'=>$email, 'password'=>md5($password), 'verified' => 1)

        );
        $TblUser = new TblUser();
        $result = $TblUser->selectRecord($condArr);
        if(!$result) {
            echo Resp::retFail(-2, 'user not exist');
            exit;
        }

        $this->sessionSaveUserId($result->id);
        $this->sessionSaveUserEmail($result->email);

        echo Resp::retSuc(1, (object)array());
    }

    public function actionRegister() {
        // http://192.168.3.69/user/register

        if(!$this->session()) {
            $this->render('register');
            exit;
        }
        $this->redirect(URL_PROJECT_INDEX);
    }

    public function actionRegisterPost() {
        // http://192.168.3.69/user/registerPost

        $email = Check::checkPostParam(-1, 'email', false);
        $password = Check::checkPostParam(-1, 'password', false);
        if(!Regular::regEmail($email)) {
            echo Resp::retFail(0, 'invalid email not right email format');
            exit;
        }

        // email registered and verified
        $condArr = array(
            'select' => array('id'),
            'condition' => 'email=:email AND verified=:verified',
            'params' => array('email' => $email, 'verified' => 1)
        );
        $TblUser = new TblUser();
        $result = $TblUser->selectRecord($condArr);
        if($result) {
            echo Resp::retFail(-2, 'email registered');
            exit;
        }

        // email registered and not verified just update
        $condArr = array(
            'select' => array('id'),
            'condition' => 'email=:email AND verified=:verified',
            'params' => array('email' => $email, 'verified' => 0)
        );
        $result = $TblUser->selectRecord($condArr);
        $verifyCode = md5(microtime());
        if($result) {// update
            $condArr = array(
                'condition' => 'email=:email',
                'params' => array('email' => $email)
            );
            $contArr = array(
                'verify_code' => $verifyCode,
                'password' => md5($password)
            );
            if (!$TblUser->updateRecord($condArr, $contArr)) {
                echo Resp::retFail(-1, 'update user failed');
                exit;
            }
        } else {// add
            $contArr = array(
                'email' => $email,
                'password' => md5($password),
                'verify_code' => $verifyCode,
                'verified' => 0
            );
            if(!$TblUser->addRecord('user', $contArr)) {
                echo Resp::retFail(-1, 'add user failed');
                exit;
            }
        }

        // send verify email
        $link = 'http://192.168.3.69/user/registerVerify/verifyCode/'.$verifyCode;
        $msg = '激活账号：';
//        $msg = '点击以下邮件激活账号：';
        $msg .= $link;
//        $title = 'Painter注册验证';
        $title = '注册验证';
        $mailer = new Mailer($title, $msg, $email);
        if(!$mailer->send()) {
            echo Resp::retFail(-3, 'send email failed');
            exit;
        }

        echo Resp::retSuc(1, (object)array());
    }

    public function actionRegisterVerify() {
        // http://192.168.3.69/roam/index.php?r=user/registerVerify

        $verifyCode = Check::checkGetParam(-1, 'verifyCode', false);

        $condArr = array(
            'select' => array('id', 'email'),
            'condition' => 'verify_code=:verifyCode AND verified=:verified',
            'params' => array('verifyCode' => $verifyCode, 'verified' => 0)
        );
        $TblUser = new TblUser();
        $result = $TblUser->selectRecord($condArr);
        if(!$result) {
            $arr = array(
                'msg' => '验证无效 ...'
            );
            $this->render('register_verify', $arr);
            exit;
        }
        $userId = $result->id;
        $email = $result->email;

        $condArr = array(
            'condition' => 'email=:email AND verified=:verified',
            'params' => array('email' => $email, 'verified' => 0)
        );
        $contArr = array(
            'verify_code' => md5($verifyCode),
            'verified' => 1
        );
        if (!$TblUser->updateRecord($condArr, $contArr)) {
            $arr = array(
                'msg' => '验证失败！ 请重新注册 ...'
            );
            $this->render('register_verify', $arr);
            exit;
        }

        $this->sessionSaveUserId($userId);
        $this->sessionSaveUserEmail($email);

        $this->redirect(URL_PROJECT_INDEX);
    }

    public function actionLogout() {
        // http://192.168.3.69/user/logout

        Yii::app()->session->clear();
        Yii::app()->session->destroy();
        $this->redirect(URL_SERVER);
    }

}
